package crawler;

import ag.webpage.WebPage;
import browser.Chorme;
import com.alibaba.fastjson.JSON;
import img.AgImgUtil;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import polestar.Feature.Feature;
import util.HtmlUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 15:27:32 2018/11/22
 * @version:2.0
 */
public class TestAg {

    public static void click(WebDriver driver, int width, int height) {
        //Point point = new Point(width,height);
        WebElement body = driver.findElement(By.tagName("html"));
        Actions action = new Actions(driver);
        action.moveToElement(body, width, height).click().perform();
        // 鼠标左键点击指定的元素
    }

    public static void guoji(WebDriver handle1) {
        // click(handle1, 194, 282);

        int[] pisitionC1 = {328, 372, 490, 1313};//C1

        int[] pisitionC3 = {328 + 220, 372, 270, 1313};//C3
        int[] pisitionC6 = {328 + 220 + 220, 372, 51, 1313};//C6
        int[] pisitionC2 = {328, 1133, 490, 552};//C2
        int[] pisitionC5 = {328 + 220, 1133, 270, 552};//C5
        int timeTop = 28;
        int timeLeft = 80;
        int timeBottom = 139;
        int timeRight = 104;
        int[] timeC1 = {pisitionC1[0] - timeTop, pisitionC1[1] + timeLeft, pisitionC1[2] + timeBottom, pisitionC1[3] + timeRight};
        int[] timeC2 = {pisitionC2[0] - timeTop, pisitionC2[1] + timeLeft, pisitionC2[2] + timeBottom, pisitionC2[3] + timeRight};
        int[] timeC3 = {pisitionC3[0] - timeTop, pisitionC3[1] + timeLeft, pisitionC3[2] + timeBottom, pisitionC3[3] + timeRight};
        int[] timeC5 = {pisitionC5[0] - timeTop, pisitionC5[1] + timeLeft, pisitionC5[2] + timeBottom, pisitionC5[3] + timeRight};
        int[] timeC6 = {pisitionC6[0] - timeTop, pisitionC6[1] + timeLeft, pisitionC6[2] + timeBottom, pisitionC6[3] + timeRight};
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //转换时间格式
        String time = dateFormat.format(Calendar.getInstance().getTime());  //获取当前时间
        File srcFile = Chorme.snapshot(handle1, true);  //执行屏幕截取
        try {

            BufferedImage bufferedImage = ImageIO.read(srcFile);
           /*
           BufferedImage bitTime1 = AgImgUtil.getPositionBufferedImageNoSelect(bufferedImage, timeC1);
            ImgUtil.savePic(bitTime1, "屏幕截图"+"/cTime.jpg");

            BufferedImage bitTime2 = AgImgUtil.getPositionBufferedImageNoSelect(bufferedImage, timeC2);
            ImgUtil.savePic(bitTime2, "屏幕截图"+"/c2Time.jpg");

            BufferedImage bitTime3 = AgImgUtil.getPositionBufferedImageNoSelect(bufferedImage, timeC3);
            ImgUtil.savePic(bitTime3, "屏幕截图"+"/c3Time.jpg");

            BufferedImage bitTime5 = AgImgUtil.getPositionBufferedImageNoSelect(bufferedImage, timeC5);
            ImgUtil.savePic(bitTime5, "屏幕截图"+"/c5Time.jpg");

            BufferedImage bitTime6 = AgImgUtil.getPositionBufferedImageNoSelect(bufferedImage, timeC6);
            ImgUtil.savePic(bitTime6, "屏幕截图"+"/c6Time.jpg");*/
           /* String time1 = tesseract.Test.getResult(new File("屏幕截图"+"/cTime.jpg"));;
            String time2 = tesseract.Test.getResult(new File("屏幕截图"+"/c2Time.jpg"));;
            String time3 = tesseract.Test.getResult(new File("屏幕截图"+"/c3Time.jpg"));;
            String time5 = tesseract.Test.getResult(new File("屏幕截图"+"/c5Time.jpg"));;
            String time6 = tesseract.Test.getResult(new File("屏幕截图"+"/c6Time.jpg"));;*/

            BufferedImage bit1 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionC1);
            BufferedImage bit2 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionC2);
            BufferedImage bit3 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionC3);
            BufferedImage bit5 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionC5);
            BufferedImage bit6 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionC6);
            ArrayList listC1 = AgImgUtil.writeSingle(bit1);
            ArrayList listC2 = AgImgUtil.writeSingle(bit2);
            ArrayList listC3 = AgImgUtil.writeSingle(bit3);
            ArrayList listC5 = AgImgUtil.writeSingle(bit5);
            ArrayList listC6 = AgImgUtil.writeSingle(bit6);
            Map map = new HashMap();
            map.put("c1", Feature.performWithoutEqual(listC1));
            map.put("c2", Feature.performWithoutEqual(listC2));
            map.put("c3", Feature.performWithoutEqual(listC3));
            map.put("c5", Feature.performWithoutEqual(listC5));
            map.put("c6", Feature.performWithoutEqual(listC6));
            map.put("c1equal", Feature.performWithEqual(listC1));
            map.put("c2equal", Feature.performWithEqual(listC2));
            map.put("c3equal", Feature.performWithEqual(listC3));
            map.put("c5equal", Feature.performWithEqual(listC5));
            map.put("c6equal", Feature.performWithEqual(listC6));
            map.put("time", time);
           /* map.put("c1time",time1);
            map.put("c2time",time2);
            map.put("c3time",time3);
            map.put("c5time",time5);
            map.put("c6time",time6);*/
            String r = URLEncoder.encode(JSON.toJSONString(map), "UTF-8");
            HtmlUtil.get("http://localhost/test/setAg?map=" + r);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void qijian(WebDriver handle1) {
        click(handle1, 194, 332);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int[] pisitionB1 = {328, 372, 490, 1313};//C1
        int[] pisitionB3 = {328 + 220, 372, 270, 1313};//C5
        int[] pisitionB5 = {328 + 220 + 220, 372, 51, 1313};//C6
        int[] pisitionB2 = {328, 1133, 490, 552};//C2
        int[] pisitionB4 = {328 + 220, 1133, 270, 552};//C3
        int[] pisitionB6 = {328 + 220 + 220, 1133, 51, 552};//C5
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");  //转换时间格式
        String time = dateFormat.format(Calendar.getInstance().getTime());  //获取当前时间
        File srcFile = ((TakesScreenshot) handle1).getScreenshotAs(OutputType.FILE);  //执行屏幕截取
        try {
            BufferedImage bufferedImage = ImageIO.read(srcFile);
            BufferedImage bit1 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionB1);
            BufferedImage bit3 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionB3);
            BufferedImage bit5 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionB5);
            BufferedImage bit2 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionB2);
            BufferedImage bit4 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionB4);
            BufferedImage bit6 = AgImgUtil.getPositionBufferedImage(bufferedImage, pisitionB6);
            ArrayList listB1 = AgImgUtil.writeSingle(bit1);
            ArrayList listB2 = AgImgUtil.writeSingle(bit2);
            ArrayList listB3 = AgImgUtil.writeSingle(bit3);
            ArrayList listB4 = AgImgUtil.writeSingle(bit4);
            ArrayList listB5 = AgImgUtil.writeSingle(bit5);
            ArrayList listB6 = AgImgUtil.writeSingle(bit6);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        int windowWidth = 1920;
        int windowHeight = 1080;
        WebDriver driver = Chorme.open(windowWidth, windowHeight, true);//open();
        //  enterTry(driver);
        WebDriver handle1 = Chorme.changeWindow(1, driver);
        WebPage.waitPage(30000);
        while (true) {
            try {
                guoji(handle1);
            } catch (Exception e) {
                System.out.println("出错了" + e);
            }

            // qijian(handle1);
            try {
                Thread.sleep(800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
